package com.neusoft.demo3;

import java.util.Scanner;

public class TestBook {
    /**
     * 打印图书信息
     * @param books
     */
    public void printBooksInfo(Book[] books) {
        //遍历books数组并输出图书的信息
        for(int i = 0 ;i<books.length;i++) {
            Book bk = books[i];
            System.out.println(bk.getBid() + "\t" + bk.getBname() + "\t" + bk.getPrice() + "\t" + bk.getStorage());
        }
    }

    /**
     * 购买图书
     * @param books
     */
    public Order buyBooks(Book[] books) {
        Scanner input = new Scanner(System.in);
        //创建一个订单，手动给一个订单编号0001
        Order order = new Order("0001");
        for (int i = 0;i < 3;i++) { //循环3次接收用户的输入
            //输入编号以及购买的数量
            System.out.println("请输入图书的编号: ");
            int pid = input.nextInt();
            System.out.println("请输入图书的购买数量: ");
            int pnum = input.nextInt();
            //创建明细OrderItem对象
            OrderItem orderItem = new OrderItem(books[pid - 1].getBname(),books[pid - 1].getPrice(),pnum);
            //并将这个对象保存到数组中
            //调用订单对象中的方法来保存明细对象
            order.setItem(orderItem,i);
        }
        //返回订单对象
        return order;
    }

    public static void main(String[] args) {
        Book[] books = new Book[3];
        Book b1 = new Book(1,"Java",56.50,50);
        Book b2 = new Book(2,"Web开发",57.50,150);
        Book b3 = new Book(3,"JavaScript",58.50,250);
        books[0] = b1;
        books[1] = b2;
        books[2] = b3;
        //调用printBooksInfo方法
        TestBook testBook = new TestBook();
        testBook.printBooksInfo(books);

        //调用buyBooks方法
        //Order order = testBook.buyBooks(books);
        Order order = new Order("1000");
        System.out.println(order.getOrderId());
        System.out.println(order.getTotal());

        //输出明细列表
        OrderItem[] items = order.getItems();
        for (OrderItem item:items) {
            System.out.println(item.getPname()+"----"+item.getPprice()+"----"+item.getNum());
        }

    }
}
